﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class ClassRoomController : Controller
    {
        public ActionResult ClassRoomList()
        {
            return View();
        }
        //查询
        public ActionResult List(BD_ClassRoom CR)
        {
            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            listSearch.Add(new SearchCondition() { Field = "DelState", Operator = "=", Value = "0" });
            Expression<Func<BD_ClassRoom, bool>> exp = SearchExpression.FindByGroup<BD_ClassRoom>(CR, listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            //var list = BLLSession.Current.BD_ClassRoomBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            //count = BLLSession.Current.BD_ClassRoomBLL.GetCount(exp);
            var list = BLLSession.Current.BD_ClassRoomBLL.GetPagedListSchool(OperateContext.Page, OperateContext.Rows, exp, t => t.ID, true);
            count = BLLSession.Current.BD_ClassRoomBLL.GetCountSchool(exp);
            return Json(new EasyuiDataPager() { rows = list, total = count });
        }
        //添加
        public ActionResult AddClassRoom(BD_ClassRoom CR)
        {
            var list1 = DALSession.Current.BD_ClassRoomDAL.GetListBy(d => d.ClassRoom == CR.ClassRoom && d.SchoolCode == OperateContext.sessUserInfo.SchoolCode && d.DelState == "0");
            if (list1.Count == 0)
            {
                CR.SchoolCode = OperateContext.sessUserInfo.SchoolCode;
                CR.SchoolName = OperateContext.sessUserInfo.SchoolName;
                CR.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                CR.EditPerName = OperateContext.sessUserInfo.Name;
                CR.EditPerID = OperateContext.sessUserInfo.Id;
                CR.DelState = "0";
                BLLSession.Current.BD_ClassRoomBLL.Add(CR);
                return Json(new Message() { msg = "保存成功", state = "success" });
            }
            else
            {
                return Json(new Message() { msg = "数据已存在！", state = "warning" });
            }
        }
        //修改
        public ActionResult EditClassRoom(BD_ClassRoom CR)
        {
            CR.SchoolCode = OperateContext.sessUserInfo.SchoolCode;
            CR.SchoolName = OperateContext.sessUserInfo.SchoolName;
            CR.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            CR.EditPerName = OperateContext.sessUserInfo.Name;
            CR.EditPerID = OperateContext.sessUserInfo.Id;
            BLLSession.Current.BD_ClassRoomBLL.ModifyBy(CR, t => t.ID == CR.ID, "ClassRoom", "SchoolCode", "SchoolName", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "保存成功", state = "success" });
        }
        //删除
        public ActionResult DelClassRoom(int Id)
        {
            //BLLSession.Current.BD_ClassRoomBLL.DelBy(d => d.ID == Id);
            BD_ClassRoom BD_ClassRoom1 = DALSession.Current.BD_ClassRoomDAL.GetListBy(d => d.ID == Id).FirstOrDefault();
            BD_ClassRoom1.EditTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            BD_ClassRoom1.EditPerName = OperateContext.sessUserInfo.Name;
            BD_ClassRoom1.EditPerID = OperateContext.sessUserInfo.Id;
            BD_ClassRoom1.DelState = "1";
            BLLSession.Current.BD_ClassRoomBLL.ModifyBy(BD_ClassRoom1, t => t.ID == BD_ClassRoom1.ID, "ClassRoom", "EditPerID", "EditPerName", "EditTime", "Note", "DelState");
            return Json(new Message() { msg = "删除成功", state = "success" });
        }
        //获取所有校区信息
        public void GetAllSchoolInfo()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_SchoolInfoDAL.GetListBy(t => t.DelState == "0").Select(t => new { value = t.SchoolCode, text = t.SchoolName })));
        }
        //获取所有教师编号
        public void GetAllClassRoom()
        {
            Response.Write(JsonHelper.toJson(DALSession.Current.BD_ClassRoomDAL.GetListBy(t => t.DelState == "0" && t.SchoolCode == OperateContext.sessUserInfo.SchoolCode).Select(t => new { value = t.ClassRoom, text = t.ClassRoom })));
        }
    }
}
